﻿using Dapper.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using YouWei.Api.Common;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Service;
using YouWei.Utility;

namespace YouWei.Api.Controllers
{
    /// <summary>
    /// 用户组
    /// </summary>
    public class UserGroupController : BaseApiController
    {
        private UserGroupService _service = new UserGroupService();
        private UserGroupRelService _serviceRel = new UserGroupRelService();
        private WxGroupUserService _serviceGroupUser = new WxGroupUserService();
        /// <summary>
        /// 获取用户所有分组
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<Page<UserGroup>> GetAllGroup(int UserID)
        {
            ResultData<Page<UserGroup>> result = ResultData<Page<UserGroup>>.Success();
            result.data = _service.Pages(1, 10000, UserID, "GroupID", "DESC");
            return result;
        }

        /// <summary>
        /// 添加分组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> AddGroup(UserGroup entity)
        {
            ResultData<int> result = ResultData<int>.Success("添加成功");
            if (entity.UserID <= 0)
            {
                result.status = -1;
                result.message = "未查找到对应用户";
                return result;
            }
            if (string.IsNullOrWhiteSpace(entity.GroupName))
            {
                result.status = -1;
                result.message = "组名不能为空";
                return result;
            }
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DefultTime;
            entity.IsActvie = 1;
            result.data = EConvert.ToInt(_service.Insert(entity));
            return result;
        }

        /// <summary>
        /// 更新/修改分组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<string> UpdateGroup(UserGroup entity)
        {
            var result = ResultData<string>.Success("修改成功");
            if (entity.GroupID <= 0)
            {
                result.status = -1;
                result.message = "缺少必要的参数ProductID";
                return result;
            }
            var model = _service.Single(entity.GroupID);
            if (model == null)
            {
                result.status = -1;
                result.message = "修改失败，信息不存在";
                return result;
            }
            entity.CreateDate = model.CreateDate;
            entity.IsActvie = model.IsActvie;
            entity.UpdateDate = DateTime.Now;
            //entity.MergeTo(model);
            var r = _service.Update(entity);
            if (!r)
            {
                result.status = -1;
                result.message = "修改失败";
            }
            return result;
        }

        /// <summary>
        /// 删除分组
        /// </summary>
        /// <param name="GroupID">主键ID</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<string> DeleteGroup([FromUri] int GroupID)
        {
            ResultData<string> result = ResultData<string>.Success("删除成功");
            if (GroupID <= 0)
            {
                result.status = -1;
                result.message = "删除失败，缺少必要的参数";
            }
            var model = _service.Single(GroupID);
            if (model != null)
            {
                model.IsActvie = 0;
                model.UpdateDate = DateTime.Now;
                var r = _service.Update(model);
                if (!r)
                {
                    result.status = -1;
                    result.message = "删除失败";
                }
            }
            else
            {
                result.status = -1;
                result.message = "删除失败，未找到要删除的组";
            }
            return result;
        }

        /// <summary>
        /// 分配用户到组
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> AddUserGroupRel(UserGroupRel entity)
        {
            ResultData<int> result = ResultData<int>.Success("设置成功");
            entity.IsActvie = 1;
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DefultTime;
            var id = EConvert.ToInt(_serviceRel.Insert(entity));
            result.data = id;
            return result;
        }

        /// <summary>
        /// 根据用户ID返回所属群组ID列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<int>> GetUserCarGroupIdList(int userId)
        {
            ResultData<List<int>> result = ResultData<List<int>>.Success("获取成功");
            List<int> list = _serviceGroupUser.GetCarGroupIdByUserId(userId);
            result.data = list;
            return result;
        }

    }
}
